//
//  ShareCustom.h
//  分享
//
//  Created by Looper on 15/9/14.
//  Copyright (c) 2015年 Looper. All rights reserved.
//

#import "LZWCustomActionSheet.h"
#import "UMSocial.h"

#define WINDOW_COLOR [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]
#define ANIMATE_DURATION                        0.25f
#define ACTIONSHEET_BACKGROUNDCOLOR             [UIColor colorWithRed:106/255.00f green:106/255.00f blue:106/255.00f alpha:0.8]



@implementation LZWCustomActionSheet

+(id)shareinstance
{
    static LZWCustomActionSheet *sc = nil;
    if(sc == nil)
    {
        sc = [[LZWCustomActionSheet alloc]init];
    }
    return sc;
}

-(id)initWithView:(UIView *)view AndHeight:(float)height{
    self = [super init];
    if (self) {
        
        //初始化背景视图，添加手势
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        self.backgroundColor = WINDOW_COLOR;
//        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        [self addGestureRecognizer:tapGesture];
        
        //生成LZWActionSheetView
        self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height - 200), 320, height)];
        self.backGroundView.backgroundColor = ACTIONSHEET_BACKGROUNDCOLOR;
        
        toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        toolBar.barStyle = UIBarStyleDefault;
        
        
        UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonLeft.frame = CGRectMake(0, 0, 40 , 25);
        [buttonLeft addTarget:self action:@selector(docancel) forControlEvents:UIControlEventTouchUpInside];
        [buttonLeft setTitle:@"取消" forState: UIControlStateNormal];
        buttonLeft.titleLabel.textColor = [UIColor blueColor];
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithCustomView:buttonLeft];
        
        UIButton *buttonRight = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonRight.frame = CGRectMake(0, 0, 40 , 25);
        [buttonRight addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
        [buttonRight setTitle:@"完成" forState: UIControlStateNormal];
        buttonRight.titleLabel.textColor = [UIColor blueColor];
        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithCustomView:buttonRight];
        
        UIBarButtonItem *fixedButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
        NSArray *array = [[NSArray alloc] initWithObjects: leftButton,fixedButton,rightButton, nil];
        [toolBar setItems: array];

        
        //给LZWActionSheetView添加响应事件(如果不加响应事件则传过来的view不显示)
        UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedBackGroundView)];
        [self.backGroundView addGestureRecognizer:tapGesture1];
        
        
        [self addSubview:self.backGroundView];
        [self.backGroundView addSubview:toolBar];
        [self.backGroundView addSubview:view];
        
        [UIView animateWithDuration:ANIMATE_DURATION animations:^{
            [self.backGroundView setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-height, [UIScreen mainScreen].bounds.size.width, height)];
            
        } completion:^(BOOL finished) {
            
        }];
    }
    
    return self;
}

- (id)ShareQQWithFrame:(CGRect)frame andImageNormal:(UIImage *)imageNormal andImageSelected:(UIImage *)imageSelected andTag:(NSInteger)tag
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setImage:imageNormal forState:UIControlStateNormal];
    [button setImage:imageSelected forState:UIControlStateSelected];
    [button addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = tag;
    return button;
}

- (id)ShareWeiXinWithFrame:(CGRect)frame andImageNormal:(UIImage *)imageNormal andImageSelected:(UIImage *)imageSelected andTag:(NSInteger)tag
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setImage:imageNormal forState:UIControlStateNormal];
    [button setImage:imageSelected forState:UIControlStateSelected];
    [button addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = tag;
    return button;
}

- (id)ShareQzoneWithFrame:(CGRect)frame andImageNormal:(UIImage *)imageNormal andImageSelected:(UIImage *)imageSelected andTag:(NSInteger)tag
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setImage:imageNormal forState:UIControlStateNormal];
    [button setImage:imageSelected forState:UIControlStateSelected];
    [button addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = tag;
    return button;
}

- (id)ShareWeiboWithFrame:(CGRect)frame andImageNormal:(UIImage *)imageNormal andImageSelected:(UIImage *)imageSelected andTag:(NSInteger)tag
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setImage:imageNormal forState:UIControlStateNormal];
    [button setImage:imageSelected forState:UIControlStateSelected];
    [button addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = tag;
    return button;
}

- (void)share:(UIButton *)button
{
    NSLog(@"button.tag = %ld",(long)button.tag);
    //NSString *shareText = [NSString stringWithFormat:@"有个应用工%@好好玩哦, 大家快来玩吧,地址是%@",self.model.name,self.model.itunesUrl];
    NSString *shareText = [NSString stringWithFormat:@"有个应用好好玩哦, 大家快来玩吧,地址是12345"];
    [[UMSocialControllerService defaultControllerService] setShareText:shareText shareImage:nil socialUIDelegate:self];        //设置分享内容和回调对象
    
    //NSArray *sharePlatforms = @[UMShareToSina,UMShareToTencent,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone];
    NSArray *sharePlatforms = @[UMShareToQQ,UMShareToWechatTimeline,UMShareToQzone,UMShareToSina];
    
    //选择分享方式进行分享
    [UMSocialSnsPlatformManager getSocialPlatformWithName:sharePlatforms[button.tag]].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
}


- (void)tappedCancel
{
    [UIView animateWithDuration:ANIMATE_DURATION animations:^{
        [self.backGroundView setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 0)];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

-(void)showInView:(UIView *)view{
    [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self];
}

- (void)tappedBackGroundView
{
    //
}

-(void)done
{
    NSLog(@"完成");
    [self.doneDelegate done];
    [self tappedCancel];
}

-(void)docancel
{
    NSLog(@"返回");
    [self tappedCancel];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
